package com.yoshino.leetcode.improve40.eleventh;

import java.util.*;

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        int[] point = new int[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103};
        // 使用权重，不同质数相乘的结果不一样
        Map<Long, List<String>> maps = new HashMap<>();
        for (String str : strs) {
            long k = 1;
            for (int i = 0; i < str.length(); i++) {
                k *= point[str.charAt(i) - 'a'];
            }
            List<String> temp = maps.getOrDefault(k, new ArrayList<>());
            temp.add(str);
            maps.put(k, temp);
        }
        return new ArrayList<>(maps.values());
    }

    public boolean isAlienSorted(String[] words, String order) {
        int[] point = new int[26];
        for (int i = 0; i < order.length(); i++) {
            point[order.charAt(i) - 'a'] = i;
        }
        for (int i = 1; i < words.length; i++) {
            int min = Math.min(words[i].length(), words[i - 1].length());
            for (int j = 0; j < min; j++) {
                int pre = point[words[i - 1].charAt(j) - 'a'];
                int cur = point[words[i].charAt(j) - 'a'];
                if (pre > cur) {
                    return false;
                } else if (pre < cur) {
                    break;
                }
                // 相等，查看长度
                if (j == min - 1 && words[i - 1].length() > min) {
                    return false;
                }
            }
        }
        return true;
    }

    public int findMinDifference(List<String> timePoints) {
        if (timePoints.size() > 24 * 60) {
            return 0;
        }
        List<Integer> point = new ArrayList<>();
        for (String timePoint : timePoints) {
//            String[] split = timePoint.split(":");
//            int size = Integer.parseInt(split[1]);
//            point.add(size + Integer.parseInt(split[0]) * 60);
            int size = Integer.parseInt(timePoint.substring(3));
            point.add(size + Integer.parseInt(timePoint.substring(0,3)) * 60);
        }
        Collections.sort(point);
        int min = point.get(1) - point.get(0);
        for (int i = 2; i < point.size(); i++) {
            min = Math.min(point.get(i) - point.get(i - 1), min);
        }
        // 最大时间和最小时间的 时间差
        min = Math.min(min, 24 * 60 - point.get(point.size() - 1) + point.get(0));
        return min;
    }
}